﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SalePartial.Utility;
using Telerik.Web.UI;
using SalePartial.BLL;
using System.Data;
using System.Drawing;
using SalePartial.Model;

public partial class NewsManage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //修改页头
            Page.Header.Title = String.Format("环球零件资源网--网站管理向导");

             // 检查用户权限并存入使用记录。
            LoginUserType type = RoleControlManager.checkUserAuthority(User.Identity.Name);
            if (type != LoginUserType.ADMIN)
            {
                //string radalertscript = "<script language='javascript'>function f(){radalert('Welcome to RadWindow for <strong>ASP.NET AJAX</strong>!', 330, 210); Sys.Application.remove_load(f);}; Sys.Application.add_load(f);</script>";

                //string radalertscript = "<script language='javascript'>function f(){radopen(\"../../../Login/NoPermission.aspx\", null); Sys.Application.remove_load(f);}; Sys.Application.add_load(f);</script>";
                //Page.ClientScript.RegisterStartupScript(this.GetType(), "radalert", radalertscript);

                //Response.Redirect(GlobalSetting.UrlHomePage, true);
            }


        }
    }

    protected void Btn_Set_Click(object sender, EventArgs e)
    {
        sdsNewsList.SelectParameters["UserName"].DefaultValue =
            tbUserName.Text.Trim() == string.Empty ? "%" : "%" + tbUserName.Text.Trim() + "%";
        sdsNewsList.SelectParameters["createStDay"].DefaultValue =
            dpCreateStDay.SelectedDate == null ? "19000101" : string.Format("{0:yyyyMMdd}", dpCreateStDay.SelectedDate);
        sdsNewsList.SelectParameters["createEdDay"].DefaultValue =
            dpCreateEdDay.SelectedDate == null ? "20991231" : string.Format("{0:yyyyMMdd}", dpCreateEdDay.SelectedDate);
        sdsNewsList.SelectParameters["CaID"].DefaultValue =
           dplCatagory.Text.Trim() == string.Empty ? "%" : dplCatagory.SelectedValue;
        sdsNewsList.SelectParameters["Title"].DefaultValue =
            tbNewTitle.Text.Trim() == string.Empty ? "%" : "%" + tbNewTitle.Text.Trim() + "%";

        sdsNewsList.DataBind();
    }


    protected void radGridMainlist_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        try
        {
            string NewsID = null;
            if (e.Item.OwnerTableView.Name == "MainTable1")
            {
                NewsID= e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["NewsID"].ToString();
            }
            else if (e.Item.OwnerTableView.Name == "DetailTable1")
            {
                NewsID = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["CoID"].ToString();
            }

            switch (e.CommandName.ToLower())
            {
                case "update":
                    HttpContext.Current.Session["NewsID"] = NewsID;
                    Response.Redirect(e.CommandArgument.ToString(), false);
                    HttpContext.Current.Session["NewsReadOnly"] = true;
                    break;
                case "view":
                    HttpContext.Current.Session["NewsID"] = NewsID;
                    HttpContext.Current.Session["NewsReadOnly"] =false;
                    Response.Redirect(e.CommandArgument.ToString(), false);
                    break;
                case "delete":

                    if (e.Item.OwnerTableView.Name == "MainTable1")
                    {
                        try
                        {
                            XPNews cm = new XPNews();
                            XPNewsInfo info = new XPNewsInfo();
                            info.NewsID = new Guid(NewsID);
                            cm.Delete(info);
                        }
                        catch (Exception)
                        {

                            Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<Script language='javascript'>alert('删除失败！');</script>");
                            return;
                        }

                        radGridMainlist.DataBind();
                    }
                    else if (e.Item.OwnerTableView.Name == "DetailTable1")
                    {
                        try
                        {
                            XPNewsComment cm = new XPNewsComment();
                            XPNewsCommentInfo info = new XPNewsCommentInfo();
                            info.CoID = new Guid(NewsID);
                            cm.Delete(info);
                        }
                        catch (Exception)
                        {

                            Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<Script language='javascript'>alert('删除失败！');</script>");
                            return;
                        }
                    }
                    break;

                case "expandcollapse":

                    break;
            }
        }
        catch (Exception ex)
        {
            e.Canceled = true;
        }
        //e.Item.OwnerTableView.Rebind();
    }

    protected void radGridMainlist_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem gdataitem = (GridDataItem)e.Item;

            //string InquiryStatus = gdataitem.GetDataKeyValue("InquiryStatus").ToString();

            //string qstatus = (((LinkButton)gdataitem["Assign"].FindControl("lbtAssign"))).CommandArgument.ToString();

            //if (InquiryStatus.Substring(4, 1) == "1" && qstatus.Substring(3, 1) == "1")
            //{
            //    gdataitem.BackColor = Color.Orange;
            //    gdataitem.ToolTip = "已授盘";
            //}
        }
    }


    protected void lbtnDel_Click(object sender, EventArgs e)
    {
        string sid = ((LinkButton)sender).CommandArgument;
        Guid id = new Guid(sid);

        try{
        new XPNews().Delete(id);
        }
        catch(Exception)
        {
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script>alert('删除失败!');</script>");
            return;
        }
        BindNews();
    }

    protected void CommentLbtnDel_Click(object sender, EventArgs e)
    {
        LinkButton lb = (LinkButton)sender;
        XPNewsComment cm = new XPNewsComment();
        if (lb.CommandArgument != null && lb.CommandArgument != string.Empty)
        {
            XPNewsCommentInfo info = new XPNewsCommentInfo();
            info.CoID = new Guid(lb.CommandArgument);

            try
            {
                cm.Delete(info);
            }
            catch (Exception)
            {

                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<Script language='javascript'>alert('删除失败！');</script>");
                return;
            }

            sdsCommentDetail.DataBind();

        }
    }

    protected void lbtnEdit_Click(object sender, EventArgs e)
    {
        string sid = ((LinkButton)sender).CommandArgument;
        int id = Convert.ToInt32(sid);
        Response.Redirect("~/Admin/addnews.aspx?id=" + id.ToString());
    }

    private void BindNews()
    {
        DataSet da = new XPNews().SelectDynamic(null, null);
        DataSet ds = new DataSet();
        //da.Fill(ds, AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1), AspNetPager1.PageSize, "temptbl");
        //DataTable dt = ds.Tables["temptbl"];
        //repNews.DataSource = dt;

        //repNews.DataSource = da;
        //repNews.DataBind();
    }

}
